<template>
    <view>
        <button size="default" type="primary" onTap="go">Button</button>
        <view>
            <text v-for="(account, index) in accounts" :key="index">{{ account }}</text>
        </view>
    </view>
</template>

<script>
	import {
		ref,
		onMounted
	} from 'vue';
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app';
	import {
		onPullDownRefresh,
		onReachBottom
	} from "@dcloudio/uni-app";
	
export default {
    data() {
        return {
            account: 'nihao',
            genders: 'nihao',
            openID: 'admin',
            address: 'nihao',
            accounts: ''
        };
    },
    methods: {
        go() {
            let that = this;

            // Log the data being sent to ensure they are not null
            console.log('Sending data:', {
                account: that.account,
                genders: that.genders,
                address: that.address
            });
            uni.request({
                url: 'http://localhost:5657/updateByAliUser',
                // Ensure this URL is correct
                method: 'POST',
                data: {
                    // Include necessary user data here, for example:
                    openID: that.openID,
                    account: that.account,
                    genders: that.genders,
                    address: that.address
                },
                headers: {
                    'content-type': 'application/json' // Default value
                },
                dataType: 'json',
                success: function (res) {
                    console.log('Response data:', res.data); // Log the response data for debugging

                    if (res.data && res.data.code === 0) {
                        // Assuming the API returns the updated accounts
                        that.setData({
                            accounts: res.data.data.accounts
                        });
                        uni.alert({
                            content: '更新成功'
                        });
                    } else {
                        uni.alert({
                            content: '更新失败: ' + (res.data.message || '未知错误')
                        });
                    }
                },
                fail: function (error) {
                    console.error('fail: ', JSON.stringify(error));
                    uni.alert({
                        content: '请求失败: ' + JSON.stringify(error)
                    });
                },
                complete: function (res) {
                    uni.hideLoading();
                }
            });
        }
    }
};
</script>
<style>

</style>
